using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using Common.Unity.Extensions.PropertyMapper.Policies;
using Microsoft.Practices.ObjectBuilder2;

namespace Common.Unity.Extensions.PropertyMapper.Policies
{
	class PropertyMappingExpressionsPolicy : PropertyMappingPolicyBase, IMirroredPropertyMappingPolicy
	{
		readonly IEnumerable<PropertyExpressionMapping> expressions;

		public PropertyMappingExpressionsPolicy( IEnumerable<PropertyExpressionMapping> expressions )
		{
			this.expressions = expressions;
		}

		public IEnumerable<PropertyExpressionMapping> Expressions
		{
			get { return expressions; }
		}

		IPropertyMappingPolicy IMirroredPropertyMappingPolicy.CreateMirror()
		{
			PropertyMappingExpressionsPolicy result = new PropertyMappingExpressionsPolicy( CreateMirroredExpressions() );
			return result;
		}

		protected IEnumerable<PropertyExpressionMapping> CreateMirroredExpressions()
		{
			foreach ( PropertyExpressionMapping mapping in expressions )
			{
				yield return mapping.CreateMirror();
			}
		}

		protected override IEnumerable<IPropertySetterInfo> ResolveProperties( PropertyMappingContext context )
		{
			foreach ( PropertyExpressionMapping mapping in expressions )
			{
				yield return mapping.CreateProperty( context );
			}
		}
	}
}